package so.ifl.manager.admin;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.except.SessionTimeOutException;
import so.ifl.servcie.OfficeService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.OfficeVo;

/**
 * 办事处信息manager
 * create by simon
 * create at 2012-1-10 下午5:20:42
 * <mailto:liangyuanliu@gmail.com>
 */
@Component("officeManager")
public class OfficeManager {
	
	@Autowired
	private OfficeService officeService;

	private Logger logger_ = LoggerFactory.getLogger(this.getClass());


	/**
	 *  办事处信息分页查询
	 * simon
	 * create at 2012-1-10 下午5:20:27
	 * <mailto:liangyuanliu@gmail.com>
	 * @param page
	 * @param map
	 */
	@DataProvider
	public void getOffice(Page<OfficeVo> page, Map<String, Object> map) {
		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (map == null) {
			map = new HashMap<String, Object>();
		}

		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			map.put("company_id", accountVo.getCompanyId());//获取当前账户所属的公司
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}
		
		List<OfficeVo> list = officeService.queryOfficeWithParamAndPagination(map, offset, limmit);
		int countsql = officeService.countOfficeWithParam(map);
		page.setEntities(list);
		page.setEntityCount(countsql);
	}

	/**
	 * 办事处信息curd
	 * simon
	 * create at 2012-1-10 下午5:40:20
	 * <mailto:liangyuanliu@gmail.com>
	 * @param offices
	 */
	@DataResolver
	public void updateOffice(Collection<OfficeVo> offices) {
		for (OfficeVo office : offices) {
			EntityState entityState = EntityUtils.getState(office);
			if (EntityState.NEW.equals(entityState)) {
				// 新增
				logger_.info("新增");
				insertOffice(office);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				// 修改
				logger_.info("修改");
				updateOffice(office);
			} else if (EntityState.DELETED.equals(entityState)) {
				// 删除
				logger_.info("删除");
				deleteOffice(office.getOfficeId());
			}
		}
	}

	/**
	 * 新增办事处信息
	 * simon
	 * create at 2012-1-10 下午5:41:21
	 * <mailto:liangyuanliu@gmail.com>
	 * @param consult
	 */
	private void insertOffice(OfficeVo office) {
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			office.setCompanyId(accountVo.getCompanyId());
			office.setCreateUser(accountVo.getAccountLoginName());
			office.setCreateTime(new Date());
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}
		officeService.insertOffice(office);
	}
	
	/**
	 * 删除办事处信息
	 * simon
	 * create at 2012-1-10 下午5:41:41
	 * <mailto:liangyuanliu@gmail.com>
	 * @param officeId
	 */
	private void deleteOffice(Integer officeId)
	{
		officeService.deleteOffice(officeId);		
	}
	
	/**
	 * 修改办事处信息
	 * simon
	 * create at 2012-1-10 下午5:41:52
	 * <mailto:liangyuanliu@gmail.com>
	 * @param office
	 */
	private void updateOffice(OfficeVo office)
	{
		//Map<String, Object> m = utilMap(office, false);
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			office.setLastModifyUser(accountVo.getAccountLoginName());
			office.setLastModifyTime(new Date());
		}else{
			throw new SessionTimeOutException("登录超时。。");
		}
		officeService.updateOffice(office);
	}
}
